package com.arsn.myf.system.controller;

import com.arsn.myf.common.util.result.Func;
import com.arsn.myf.common.util.result.R;
import com.arsn.myf.system.entity.Dict;
import com.arsn.myf.system.service.DictService;
import com.arsn.myf.system.vo.DictVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wildfly.common.annotation.NotNull;
import java.util.Arrays;

/**
 * 字典
 * @author zhongrj
 * @since 2022-08-02
 */
@RestController
@RequestMapping("/dict")
@AllArgsConstructor
@Api(value = "字典", tags = "字典")
public class DictController {

    private final DictService dictService;

    /**
     * 查询字典的分页数据
     * @param page
     * @param dict
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询字典的分页数据")
    public R list(Page<Dict> page, Dict dict){
        return R.data(dictService.page(page,new QueryWrapper<>(dict)));
    }


    /**
     * 自定义字典的分页数据
     * @param page
     * @param dict
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "自定义字典的分页数据")
    public R page(Page<DictVO> page, DictVO dict){
        return R.data(dictService.selectPageDictList(page,dict));
    }


    /**
     * 字典详情信息
     * @param dict
     * @return
     */
    @GetMapping("/detail")
    @ApiOperation(value = "字典详情信息")
    public R detail(Dict dict){
        return R.data(dictService.getOne(new QueryWrapper<>(dict)));
    }


    /**
     * 字典新增
     * @param dict
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "字典新增")
    public R save(@RequestBody Dict dict){
        return R.status(dictService.save(dict));
    }

    /**
     * 字典修改
     * @param dict
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "字典修改")
    public R update(@RequestBody Dict dict){
        return R.status(dictService.updateById(dict));
    }


    /**
     * 删除,批量删除
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ApiOperation(value = "删除,批量删除")
    public R remove(@NotNull String ids){
        return R.status(dictService.removeByIds(Func.stringToLong(Arrays.asList(ids.split(",")))));
    }

    /**
     * 查询字典
     * @param dict
     * @return
     */
    @GetMapping("/getDictList")
    @ApiOperation(value = "查询字典")
    public R getDictList(DictVO dict){
        return R.data(dictService.getDictList(dict));
    }

    /**
     * 根据字典编号查询字典
     * @param dict
     * @return
     */
    @GetMapping("/getDictListByCode")
    @ApiOperation(value = "根据字典编号查询字典")
    public R getDictListByCode(DictVO dict){
        return R.data(dictService.getDictListByCode(dict));
    }
}
